﻿
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PARK.BussinCache.Entitys.Dto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace uni.ubi.open.sdk.Uts
{
    public class WebApiHelper
    {
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="token">鉴权token</param>
        /// <param name="projectGuid">项目Id</param>
        /// <returns></returns>
        public static JObject Post(string url, string data, string token,string projectGuid)
        {
            string strResult = "";
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //加入头信息
                request.Headers.Add("token", token);
                request.Headers.Add("projectGuid", projectGuid);
                //写数据
                request.Method = "POST";
                request.ContentLength = bytes.Length;
                request.ContentType = "application/json";
                request.Referer = "";
                request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
                request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
                request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
                request.KeepAlive = true;
                Stream reqstream = request.GetRequestStream();
                reqstream.Write(bytes, 0, bytes.Length);
                //读数据
                request.Timeout = 300000;
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
                strResult = streamReader.ReadToEnd();
                //关闭流
                reqstream.Close();
                streamReader.Close();
                streamReceive.Close();
                request.Abort();
                response.Close();
                return (JObject)JsonConvert.DeserializeObject(strResult);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        //不校验ssl证书
        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {  // 总是接受
            return true;
        }
        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="webApi"></param>
        /// <param name="queryStr"></param>
        /// <param name="_appKey"></param>
        /// <param name="_timestamp"></param>
        /// <param name="_sign"></param>
        /// <returns></returns>
        public static JObject Get(string webApi, string queryStr, string _appKey,string _timestamp,string _sign,out string _outmsg)
        {
            try
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webApi); //--需要封装的参数
                request.CookieContainer = new CookieContainer();
                //加入头信息
                request.Headers.Add("appKey", _appKey);
                request.Headers.Add("timestamp", _timestamp);
                request.Headers.Add("sign", _sign);
                request.Referer = "";
                request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
                request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
                request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
                request.KeepAlive = true;
                //上面的http头看情况而定，但是下面俩必须加 
                request.ContentType = "application/json";
                request.Method = "get";  //--需要将get改为post才可行
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();  //远程服务器返回错误: (405) 不允许的方法/远程服务器返回错误: (500) 内部服务器错误。
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                string retString = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                _outmsg = retString;
                return (JObject)JsonConvert.DeserializeObject(retString);
            }
            catch (Exception ex)
            {
                _outmsg = ex.Message;
                return null;
            }
        }
    }
}
